//
//  InstallationDetailTabDetail.swift
//  ezyform
//
//  Created by MoennigQ on 26/2/19.
//  Copyright © 2019 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class InstallationDetailTabDetail: UIView {
    //model
    weak var model: PVDDetailModel!
    let scrollView = UIScrollView()
    let contentView = UIView()
    //job detail
    let jobDetailBackView = CustomBackView()
    let nameLabel = UILabel()
    let editOwnerInfoBtn = UIButton()
    var editOwnerNameBtnClickClosure: (()->Void)?
    let addressLabel = UILabel()
    var addressBtnClickClosure: (()->Void)?
    var copyAddrBtnClickClosure: (()->Void)?
    let phoneBtn = UIButton()
    let nmiLabel = UILabel()
    let installationDateLabel = UILabel()
    let editInstallationAddressBtn = UIButton()
    let propertyTypeImageView = UIImageView(image: UIImage(named: "PVDDetail_property_type"))
    let propertyTypeLabel = UILabel()
    //job site instructions
    let jobSiteInstructionsView = PVDDetailJobSiteInstructionForTheInstallerView()
    //pick up address
    let pickupAddressView = PVDDetailPickupAddressView()
    //system info view
    let systemInfoView = JobDetailSystemInfoView()
    //panel number
    let panelInfoView = PVDDetailPanelInfoView()
    //inverter
    let inverterInfoView = PVDDetailInverterInfoView()
    //battery
    let batteryInfoView = PVDDetailBatteryInfoView()
    //additional info view
    let additionalInfoView = JobDetailAdditionalInfoView()
    
    let attachmentView = PVDDetailAttachmentView()
    
    var phoneBtnClickClosure: (()->Void)?
    var editInverterBtnClosure: (()->Void)?
    //for code
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    //for storyboard
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpView()
    }
    private func setUpView() {
        self.addSubview(self.scrollView)
        self.scrollView.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        self.scrollView.addSubview(contentView)
        self.scrollView.backgroundColor = UIColor.white
        self.contentView.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(scrollView)
            make.left.right.equalTo(self)
        }
        
        self.contentView.backgroundColor = APPConfig.PVDDetailBackColor
        
        //job detail
        let jobDetailLabel = UILabel()
        jobDetailLabel.textAlignment = .center
        jobDetailLabel.font = UIFont(name: "SFProText-Regular", size: 13)
        jobDetailLabel.textColor = UIColor(hexStr: "#252224",alpha:0.7)
        self.contentView.addSubview(jobDetailLabel)
        jobDetailLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(14)
            make.width.height.greaterThanOrEqualTo(0)
        }
        jobDetailLabel.text = NSLocalizedString("pvd.detail.jobinformation", comment: "")
        //
        let leftMargin: CGFloat = 14
        //**************User Info*********//
        //job detail back
        
        self.contentView.addSubview(jobDetailBackView)
        jobDetailBackView.snp.makeConstraints { (make) in
            make.top.equalTo(jobDetailLabel.snp.bottom).offset(18)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //name label
        nameLabel.textColor = UIColor(red: 0.27, green: 0.27, blue: 0.27,alpha:1)
        nameLabel.textAlignment = .left
        nameLabel.numberOfLines = 0
        nameLabel.font = UIFont(name: "SFProDisplay-Medium", size: 17)
        jobDetailBackView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(leftMargin)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //editNameBtn
        editOwnerInfoBtn.setImage(UIImage(named: "PVDDetail_ownername_edit"), for: UIControl.State.normal)
        jobDetailBackView.addSubview(editOwnerInfoBtn)
        editOwnerInfoBtn.snp.makeConstraints { make in
            make.top.equalTo(nameLabel).offset(-10)
            make.left.equalTo(nameLabel.snp.right)
            make.right.equalToSuperview().offset(-10)
            make.height.width.equalTo(40)
        }
        //address imageview
        let addressImageView = UIImageView(image: UIImage(named: "PVDDetail_address"))
        jobDetailBackView.addSubview(addressImageView)
        addressImageView.snp.makeConstraints { (make) in
            make.width.equalTo(13)
            make.height.equalTo(15)
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(18)
        }
        //address label
        addressLabel.textColor = UIColor(hexStr: "#3BADFF")
        addressLabel.textAlignment = .left
        addressLabel.font = UIFont(name: "SFProText-Regular", size: 14)
        addressLabel.numberOfLines = 0
        jobDetailBackView.addSubview(addressLabel)
        addressLabel.snp.makeConstraints { (make) in
            make.top.equalTo(addressImageView).offset(-3)
            make.left.equalTo(addressImageView.snp.right).offset(9)
            make.right.equalToSuperview().offset(-50)
            make.height.greaterThanOrEqualTo(15)
        }
        //addressCopy btn
        let copyAddrBtn = UIButton()
        copyAddrBtn.setImage(UIImage(named: "PVDDetail_address_copy"), for: UIControl.State.normal)
        jobDetailBackView.addSubview(copyAddrBtn)
        copyAddrBtn.snp.makeConstraints { (make) in
            make.top.equalTo(addressLabel).offset(-10)
            make.left.equalTo(addressLabel.snp.right)
            make.right.equalToSuperview().offset(-10)
            make.height.width.equalTo(40)
        }
        //nmi imageview
        let nmiImageView = UIImageView(image: UIImage(named: "PVDDetail_nmi"))
        jobDetailBackView.addSubview(nmiImageView)
        nmiImageView.snp.makeConstraints { (make) in
            make.width.equalTo(13)
            make.height.equalTo(15)
            make.left.equalTo(nameLabel)
            make.top.equalTo(addressLabel.snp.bottom).offset(5)
        }
        //nmi label
        nmiLabel.textColor = UIColor(red: 0.27, green: 0.27, blue: 0.27,alpha:1)
        nmiLabel.textAlignment = .left
        nmiLabel.font = UIFont(name: "SFProText-Regular", size: 14)
        nmiLabel.numberOfLines = 0
        jobDetailBackView.addSubview(nmiLabel)
        nmiLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nmiImageView.snp.right).offset(9)
            make.right.lessThanOrEqualTo(-10)
            make.top.equalTo(nmiImageView)
            make.height.greaterThanOrEqualTo(15)
        }
        //phone imageview
        let phoneImageView = UIImageView(image: UIImage(named: "PVDDetail_phone"))
        jobDetailBackView.addSubview(phoneImageView)
        phoneImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(14)
            make.centerX.equalTo(nmiImageView)
            make.top.equalTo(nmiLabel.snp.bottom).offset(10)
        }
        //phone btn
        phoneBtn.setTitleColor(UIColor(hexStr: "#3BADFF"), for: UIControl.State.normal)
        phoneBtn.titleLabel?.font = UIFont(name: "SFProText-Regular", size: 15)
        phoneBtn.titleLabel?.textAlignment = .left
        jobDetailBackView.addSubview(phoneBtn)
        phoneBtn.snp.makeConstraints { (make) in
            make.left.equalTo(nmiLabel)
            make.centerY.equalTo(phoneImageView)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //date imageview
        let dateImageView = UIImageView(image: UIImage(named: "PVDDetail_date"))
        jobDetailBackView.addSubview(dateImageView)
        dateImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(14)
            make.left.equalTo(phoneBtn.snp.right).offset(24)
            make.centerY.equalTo(phoneImageView)
        }
        //install date label
        installationDateLabel.textColor = UIColor(red: 0.52, green: 0.52, blue: 0.52,alpha:1)
        installationDateLabel.textAlignment = .left
        installationDateLabel.font = UIFont(name: "SFProText-Regular", size: 14)
        jobDetailBackView.addSubview(installationDateLabel)
        installationDateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(dateImageView.snp.right).offset(10)
            make.centerY.equalTo(phoneImageView)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //property type
        jobDetailBackView.addSubview(propertyTypeImageView)
        propertyTypeImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(14)
            make.left.equalTo(phoneImageView)
            make.top.equalTo(phoneImageView.snp.bottom).offset(10)
        }
        //install date label
        propertyTypeLabel.textColor = UIColor(red: 0.27, green: 0.27, blue: 0.27,alpha:1)
        propertyTypeLabel.textAlignment = .left
        propertyTypeLabel.font = UIFont(name: "SFProText-Regular", size: 14)
        jobDetailBackView.addSubview(propertyTypeLabel)
        propertyTypeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(propertyTypeImageView.snp.right).offset(10)
            make.centerY.equalTo(propertyTypeImageView)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //job site instructions
        jobDetailBackView.addSubview(jobSiteInstructionsView)
        jobSiteInstructionsView.snp.makeConstraints { (make) in
            make.top.equalTo(propertyTypeLabel.snp.bottom)
            make.left.equalTo(nameLabel)
            make.right.equalTo(-10)
            make.bottom.equalTo(-20)
        }
        //system info view
        self.contentView.addSubview(systemInfoView)
        systemInfoView.snp.makeConstraints { (make) in
            make.top.equalTo(jobDetailBackView.snp.bottom).offset(32)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //*************Panel number info******//
        self.contentView.addSubview(panelInfoView)
        panelInfoView.snp.makeConstraints { (make) in
            make.top.equalTo(systemInfoView.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //*************inverter info******//
        self.contentView.addSubview(inverterInfoView)
        inverterInfoView.snp.makeConstraints { (make) in
            make.top.equalTo(panelInfoView.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //*************battery info******//
        self.contentView.addSubview(batteryInfoView)
        batteryInfoView.snp.makeConstraints { (make) in
            make.top.equalTo(inverterInfoView.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //*********addtional info view
        self.contentView.addSubview(additionalInfoView)
        additionalInfoView.snp.makeConstraints { (make) in
            make.top.equalTo(batteryInfoView.snp.bottom).offset(20)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //*************Attachments********
        //job detail
        let attachmentDetailLabel = UILabel()
        attachmentDetailLabel.text = "ATTACHMENTS"
        attachmentDetailLabel.textAlignment = .center
        attachmentDetailLabel.font = UIFont(name: "SFProText-Regular", size: 13)
        attachmentDetailLabel.textColor = UIColor(hexStr: "#252224",alpha:0.7)
        self.contentView.addSubview(attachmentDetailLabel)
        attachmentDetailLabel.snp.makeConstraints { (make) in
            make.top.equalTo(additionalInfoView.snp.bottom).offset(32)
            make.left.equalToSuperview().offset(16)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //attachment view
        self.contentView.addSubview(attachmentView)
        attachmentView.snp.makeConstraints { (make) in
            make.top.equalTo(attachmentDetailLabel.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview().offset(-20)
        }
        phoneBtn.addTarget(self, action: #selector(phoneBtnClick), for: UIControl.Event.touchUpInside)
        copyAddrBtn.addTarget(self, action: #selector(copyAddrBtnClick), for: UIControl.Event.touchUpInside)
        addressLabel.isUserInteractionEnabled = true
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(addressBtnClick))
        addressLabel.addGestureRecognizer(tapGesture)
        
        editOwnerInfoBtn.addTarget(self, action: #selector(editOwnerNameBtnClick), for: UIControl.Event.touchUpInside)
        
    }
    @objc func editOwnerNameBtnClick(){
        self.editOwnerNameBtnClickClosure?()
    }
    @objc func addressBtnClick(){
        self.addressBtnClickClosure?()
    }
    @objc func copyAddrBtnClick(){
        self.copyAddrBtnClickClosure?()
    }
    func setData(model: PVDDetailModel){
        
        self.model = model
        nameLabel.text =  model.owner_address?.type_name
        if model.canEditPVDDetailFlag{//是否可以编辑
            self.editOwnerInfoBtn.isHidden = false
        }else{//installing , unqualified可以编辑
            self.editOwnerInfoBtn.isHidden = true
        }
        addressLabel.text = model.install_address?.installation_address
        if let data = model.install_address?.grid_connection_application_ref_no, data != "" {//有才显示
            nmiLabel.text = "NMI: " + (model.install_address?.nmi ?? "") + "\nGrid Connection Application Ref No:\n" + data
        }else{
            nmiLabel.text = "NMI: " + (model.install_address?.nmi ?? "")
        }
        
        phoneBtn.setTitle(model.owner_address?.phone, for: UIControl.State.normal)
        installationDateLabel.text =  model.system_detail?.installation_date
        propertyTypeLabel.text =  model.install_address?.property_type
        //job site instruction for the installer
        if let data = model.installer_job_site_instruction, data != ""{//有内容才显示，没有内容不显示
            jobSiteInstructionsView.setData(data)
            jobSiteInstructionsView.isHidden = false
            propertyTypeLabel.snp.remakeConstraints { (make) in
                make.left.equalTo(propertyTypeImageView.snp.right).offset(10)
                make.centerY.equalTo(propertyTypeImageView)
                make.width.height.greaterThanOrEqualTo(0)
            }
            jobSiteInstructionsView.snp.remakeConstraints { (make) in
                make.top.equalTo(propertyTypeLabel.snp.bottom)
                make.left.equalTo(nameLabel)
                make.right.equalTo(-10)
                make.height.greaterThanOrEqualTo(0)
                make.bottom.equalTo(-20)
            }
        }else{
            jobSiteInstructionsView.setData("")
            jobSiteInstructionsView.isHidden = true
            propertyTypeLabel.snp.remakeConstraints { (make) in
                make.left.equalTo(propertyTypeImageView.snp.right).offset(10)
                make.centerY.equalTo(propertyTypeImageView)
                make.width.height.greaterThanOrEqualTo(0)
                make.bottom.equalTo(-20)
            }
            jobSiteInstructionsView.snp.remakeConstraints { (make) in
                make.bottom.left.right.equalTo(propertyTypeLabel)
            }
        }
        if model.pick_up_required {//true才显示，否则不显示
            //pick up addressView
            if pickupAddressView.superview == nil {
                self.contentView.addSubview(pickupAddressView)
            }
            pickupAddressView.snp.remakeConstraints { (make) in
                make.top.equalTo(jobDetailBackView.snp.bottom).offset(20)
                make.left.equalToSuperview().offset(16)
                make.right.equalToSuperview().offset(-16)
                make.height.greaterThanOrEqualTo(0)
            }
            //system info view
            systemInfoView.snp.remakeConstraints { (make) in
                make.top.equalTo(pickupAddressView.snp.bottom).offset(32)
                make.left.equalToSuperview().offset(16)
                make.right.equalToSuperview().offset(-16)
                make.height.greaterThanOrEqualTo(0)
            }
            pickupAddressView.setData(model)//设置数据
        }else{
            systemInfoView.snp.remakeConstraints { (make) in
                make.top.equalTo(jobDetailBackView.snp.bottom).offset(32)
                make.left.equalToSuperview().offset(16)
                make.right.equalToSuperview().offset(-16)
                make.height.greaterThanOrEqualTo(0)
            }
            if let _ = pickupAddressView.superview{
                pickupAddressView.removeFromSuperview()
            }
        }
        self.systemInfoView.setData(self.model)
        self.attachmentView.model = self.model
        self.attachmentView.tableView.reloadData()
        
        //*************Panel number info******//
        if let list = model.system_detail?.panels,list.count > 0{
            panelInfoView.isHidden = false
            panelInfoView.snp.remakeConstraints { (make) in
                make.top.equalTo(systemInfoView.snp.bottom).offset(20)
                make.left.equalToSuperview().offset(16)
                make.right.equalToSuperview().offset(-16)
                make.height.greaterThanOrEqualTo(0)
            }
            self.panelInfoView.refreshData(model)
        }else{
            panelInfoView.isHidden = true
            panelInfoView.snp.remakeConstraints { (make) in
                make.top.equalTo(systemInfoView.snp.bottom)
                make.left.right.equalTo(systemInfoView)
                make.height.equalTo(0)
            }
        }
        //*************inverter info******//
        if let list = model.system_detail?.inverters,list.count > 0{
            inverterInfoView.isHidden = false
            inverterInfoView.snp.remakeConstraints { (make) in
                make.top.equalTo(panelInfoView.snp.bottom).offset(20)
                make.left.equalToSuperview().offset(16)
                make.right.equalToSuperview().offset(-16)
                make.height.greaterThanOrEqualTo(0)
            }
            self.inverterInfoView.refreshData(model)
        }else{
            inverterInfoView.isHidden = true
            inverterInfoView.snp.remakeConstraints { (make) in
                make.top.equalTo(panelInfoView.snp.bottom)
                make.left.right.equalTo(panelInfoView)
                make.height.equalTo(0)
            }
        }
        //*************battery info******//
        if let list = model.system_detail?.batteries,list.count > 0{
            batteryInfoView.isHidden = false
            batteryInfoView.snp.remakeConstraints { (make) in
                make.top.equalTo(inverterInfoView.snp.bottom).offset(20)
                make.left.equalToSuperview().offset(16)
                make.right.equalToSuperview().offset(-16)
                make.height.greaterThanOrEqualTo(0)
            }
            self.batteryInfoView.refreshData(model)
        }else{
            batteryInfoView.isHidden = true
            batteryInfoView.snp.remakeConstraints { (make) in
                make.top.equalTo(inverterInfoView.snp.bottom)
                make.left.right.equalTo(inverterInfoView)
                make.height.equalTo(0)
            }
        }
        //*************additional info******//
        let additional_upgrade_system_details = (model.system_detail?.additional_upgrade_system_details ?? "").trimmingCharacters(in: .whitespaces)
        let install_additional_information = (model.system_detail?.install_additional_information ?? "").trimmingCharacters(in: .whitespaces)
        if install_additional_information != "" || additional_upgrade_system_details != ""{//有一个不为空
            self.additionalInfoView.setData(model)
            self.additionalInfoView.isHidden = false
            additionalInfoView.snp.remakeConstraints { (make) in
                make.top.equalTo(batteryInfoView.snp.bottom).offset(20)
                make.left.right.equalToSuperview()
                make.height.greaterThanOrEqualTo(0)
            }
        }else{
            self.additionalInfoView.isHidden = true
            self.additionalInfoView.snp.remakeConstraints { make in
                make.top.equalTo(batteryInfoView.snp.bottom)
                make.left.right.equalTo(batteryInfoView)
                make.height.equalTo(0)
            }
        }
    }
    @objc func phoneBtnClick(){
        self.phoneBtnClickClosure?()
    }
}

